//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-10-29  Jed Young  Creation
//

using fwt
using gfx

using chunmapModel

class PointTool : Tool
{
  |GeoPoint|? onCreateFinish
  private Int x := 0
  private Int y := 0
  private Bool click := false

  override Void actionEvent(Event e)
  {
    switch (e.id)
    {
      case EventId.mouseDown:
        x = e.pos.x
        y = e.pos.y
        click = true
      case EventId.mouseUp:
        if (click && x == e.pos.x && y == e.pos.y)
        {
          mouseClicked(e)
        }
      case EventId.mouseMove:
        if (x != e.pos.x || y != e.pos.y)
        {
          click = false
        }
      default: return
    }
  }

  private Void mouseClicked(Event e)
  {
    map.paintOnBuffer |g|
    {
      map.drawOriginImage(g, 0, 0)
      g.drawOval(x, y, 5, 5)
    }
    e.consume
    map.repaint

    p := GeoPoint(map.view.x2World(x.toFloat),map.view.y2World(y.toFloat))
    finish(p)
  }

  private Void finish(GeoPoint p)
  {
    onCreateFinish?.call(p)
  }
}